#!/bin/bash
set -e

rescue() {
    echo ERROR "Something went wrong, run with cmdline k3os.debug for more logging"
    echo Dropping to shell
    exec bash
}

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

. /usr/libexec/k3os/functions
. /usr/lib/os-release

pinfo Welcome to $PRETTY_NAME

if [ "$K3OS_DEBUG" = "true" ]; then
    set -x
fi

if ! ${SCRIPTS}/bootstrap; then
    rescue
fi

exec >/dev/console </dev/console 2>&1

reinit_debug

if ! ${SCRIPTS}/mode; then
    rescue
fi

trap rescue EXIT

export K3OS_MODE=$(</run/k3os/mode)
pinfo Running mode: ${K3OS_MODE}
source $SCRIPTS/mode-${K3OS_MODE}

pinfo Booting system
source $SCRIPTS/boot

exec /sbin/init
